home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / os2 / cenvi2.arj / FIBONACC.CMM < prev    next >
Text File  |  1993-04-02  |  1KB  |  31 lines

  1. // Fibonacci.cmm  CMM code to print fibonacci numbers until the user presses a key
  2.  
  3.  
  4. flush_keyboard() { while kbhit() getch() }
  5.  
  6. /******************************************************************************/
  7. /***********************  ARRAY FIBONACCI METHOD  *****************************/
  8. /******************************************************************************/
  9. printf("Printing Fibonacci sequence while creating an array until you press a key,\n")
  10. printf("or until we run out of memory or stack space.\n")
  11. for ( i = 0; !kbhit(); i++ )
  12.    printf("%d\t",FibArray[i] = (i < 2) ? i + 1 : FibArray[i-1] + FibArray[i-2])
  13.  
  14. flush_keyboard()
  15.  
  16. /******************************************************************************/
  17. /*********************  RECURSIVE FIBONACCI METHOD  ***************************/
  18. /******************************************************************************/
  19. printf("\nPrinting Fibonacci sequence through recursion until you press a key,\n")
  20. printf("or until we run out of memory or stack space.\n")
  21. for ( i = 1; !kbhit(); i++ )
  22.    printf("%d\t",fib(i))
  23.  
  24. flush_keyboard()
  25.  
  26. fib(n)   // return any number from fibonacci seqeunce, evaluating previous values
  27. {        // recursively if necessary
  28.    return (n <= 2) ? n : fib(n-1) + fib(n-2)
  29. }
  30.  
  31.